/**
 * @Title wheel-spring-boot-shiro-starter系统
 * @Package com.bosft.autoconfigure.shiro
 * @Description TODO
 * @Company 心医国际数字医疗系统（大连） 有限公司
 * @Copyright (c) 2016, 2017, bosft. All rights reserved.
 * @date 2018年5月16日 上午9:36:15
 * @author WangNaifei
 * @version 1.0
 */

package com.wheel.autoconfigure.shiro;

import com.wheel.autoconfigure.shiro.authorizing.AuthorizingDao;
import com.wheel.autoconfigure.shiro.exception.NoAuthorizingDaoBeanConfiguredException;
import com.wheel.autoconfigure.shiro.matcher.DefaultCredentialsMatcher;
import com.wheel.autoconfigure.shiro.realm.DefaultAuthorizingRealm;
import com.wheel.autoconfigure.shiro.session.GlobalEnterpriseCacheSessionDAO;
import com.wheel.autoconfigure.shiro.session.ShiroSessionDAO;
import com.wheel.core.utils.Log;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * {@code ShiroAutoConfiguration}
 *
 * @Description: shiro 自动配置
 *               <p>
 * @author WangNaifei, wangnaifei@163.com
 * @date 2018年5月16日 上午9:36:15
 * @version 1.0
 * @since jdk1.7+
 */
@Configuration
@PropertySource({ "classpath:application-shiro.yaml" })
@AutoConfigureBefore(org.apache.shiro.spring.config.web.autoconfigure.ShiroWebAutoConfiguration.class)
public class ShiroAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(ShiroAutoConfiguration.class);

    /**
     * @Title: ShiroAutoConfiguration
     * @Description: 构造
     */
    public ShiroAutoConfiguration() {
        Log.configured(log);
    }

    @Bean
    @ConditionalOnMissingBean
    public CredentialsMatcher credentialsMatcher() {
        return new DefaultCredentialsMatcher();
    }

    @Bean
    @ConditionalOnMissingBean
    public AuthorizingDao authorizingDao() throws NoAuthorizingDaoBeanConfiguredException {
        throw new NoAuthorizingDaoBeanConfiguredException("AuthorizingDao 必须实现");
    }

    @Bean
    @ConditionalOnMissingBean
    public Realm sysRealm(CredentialsMatcher credentialsMatcher) {
        DefaultAuthorizingRealm defaultAuthorizingRealm = new DefaultAuthorizingRealm();
        defaultAuthorizingRealm.setCredentialsMatcher(credentialsMatcher);
        return defaultAuthorizingRealm;
    }

    @Bean(name = "sessionDAO")
    @ConditionalOnMissingBean
    public SessionDAO sessionDao(ShiroSessionDAO shiroSessionDAO) {
        return new GlobalEnterpriseCacheSessionDAO(shiroSessionDAO);
    }

    @Bean
    @ConditionalOnMissingBean
    public ShiroSessionDAO shiroSessionDAO() throws NoAuthorizingDaoBeanConfiguredException {
        throw new NoAuthorizingDaoBeanConfiguredException("ShiroSessionDAO 必须实现");
    }

}
